package com.itheima.info.manager.system.controller;

import com.itheima.info.manager.system.domain.Student;
import com.itheima.info.manager.system.service.StudentService;

import java.util.Scanner;

public class StudentController {
    private static StudentService studentService = new StudentService();
    private static Scanner sc = new Scanner(System.in);

    public void start() {

        Loop:
        while (true) {
            System.out.println("--------欢迎来到 <学生> 管理系统--------");
            System.out.println("请输入您的选择: 1.添加学生  2.删除学生  3.修改学生  4.查看学生  5.退出");
            String choice = sc.next();
            switch (choice) {
                case "1":
                    //System.out.println("添加");
                    addStudnet();
                    break;
                case "2":
                    //System.out.println("删除");
                    deleteStudent();
                    break;
                case "3":
                    //System.out.println("修改");
                    updateStudent();
                    break;
                case "4":
                    //System.out.println("查询");
                    findAllStudent();
                    break;
                case "5":
                    System.out.println("感谢您使用学生管理系统, 再见!");
                    break Loop;
                default:
                    System.out.println("您的输入有误, 请重新输入");
                    break;
            }
        }
    }
//修改学生
    private void updateStudent() {
        String sid = luruxuehao();
        Student student = luruStudentInfo(sid);
        studentService.updateStudent(sid,student);
        System.out.println("修改成功");
    }
//删除学生
    private void deleteStudent() {
        String sid = luruxuehao();
        studentService.deleteStudent(sid);
        System.out.println("删除成功");
    }
//键盘录入
    public String luruxuehao() {
        Scanner sc = new Scanner(System.in);
        String sid = null;
        while (true) {
            System.out.println("请输入学号");
            sid = sc.next();
            boolean exists = studentService.isExists(sid);
            if (exists) {
               break;
            } else {
                System.out.println("不存在，请重新输入");
            }
        }
        return sid;
    }

    //查看学生
    public void findAllStudent() {
        // 1. 调用业务员中的获取方法, 得到学生的对象数组
        Student[] stus = studentService.findAllStudent();
        // 2. 判断数组的内存地址, 是否为null
        if (stus == null) {
            System.out.println("查无信息, 请添加后重试");
            return;
        }
        // 3. 遍历数组, 获取学生信息并打印在控制台
        System.out.println("学号\t\t姓名\t年龄\t生日");
        for (int i = 0; i < stus.length; i++) {
            Student stu = stus[i];
            if (stu != null) {
                System.out.println(stu.getSid() + "\t" + stu.getName() + "\t" + stu.getAge() + "\t\t" + stu.getBirthday());
            }
        }
    }
    //添加学生方法
    public void addStudnet() {
        Scanner sc = new Scanner(System.in);
        String sid = null;
        while (true) {
            System.out.println("请输入学号");
            sid = sc.next();
            boolean exists = studentService.isExists(sid);
            if (exists) {
                System.out.println("学号已被占用");
            } else {
                break;
            }
        }
        Student stu = luruStudentInfo(sid);
        boolean b = studentService.addStudent(stu);
        if (b) {
            System.out.println("添加成功");
        } else {
            System.out.println("添加失败");
        }
    }

    private Student luruStudentInfo(String sid) {
        System.out.println("请输入学生姓名:");
        String name = sc.next();
        System.out.println("请输入学生年龄:");
        String age = sc.next();
        System.out.println("请输入学生生日:");
        String birthday = sc.next();
        Student stu = new Student();
        stu.setSid(sid);
        stu.setName(name);
        stu.setAge(age);
        stu.setBirthday(birthday);
        return stu;
    }
}
